package mis.ds.ps.ui.swing;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.*;


import mis.ds.ps.model.dao.impl.DocentesDaoImpl;
import mis.ds.ps.model.dao.impl.ModulosDaoImpl;
import mis.ds.ps.model.dao.impl.VersoesDaoImpl;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Step1Module2 extends JPanel implements ActionListener {
	
	private static Logger logger = Logger.getLogger(Step1Module2.class);

	private JLabel lblModulo;
	private JLabel lblImportFile;
	private JComboBox cmbModulos;
	private JTextField txtFile;
	private JButton btnOpenFile;
	private JFileChooser fileChooser;
	private JList lstDocentes;
	private DefaultListModel listModel;
	private JScrollPane jScrollPane1;
	private JCheckBox chkCopy;
	private JComboBox cmbSemestreAnterior;
	
	
	private ISwingAdapter swingAdapter;
	
	
	/**
	 * Create the panel.
	 */
	public Step1Module2(ISwingAdapter sa) {

		PropertyConfigurator.configure("log4j.properties");

		swingAdapter = sa;
		
		initialize();

	}
	
	private void initialize() {
		// ====================================Modulo============================
		
		lblModulo = new JLabel("Modulo: ");
		cmbModulos = new JComboBox();
		fillComboBox(cmbModulos, swingAdapter.getDBModulosList());
		cmbModulos.addActionListener(this);
		// =============================== ===================================
				
		// ===============================File Chooser=======================

		lblImportFile = new JLabel("Working Import File: ");
		txtFile = new JTextField(50);
		btnOpenFile = new JButton("Open File...");
		btnOpenFile.setToolTipText("Select .xls file to import");
		fileChooser = new JFileChooser();
		btnOpenFile.addActionListener(this);
		// =============================== ===================================
		
		// ===============================Lista Docentes======================
		listModel = new DefaultListModel();		
		lstDocentes = new JList(fillList(listModel,swingAdapter.getDBDocentesList()));
		lstDocentes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lstDocentes.setVisibleRowCount(10);
		lstDocentes.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstDocentesValueChanged(evt);
            }
        });
		jScrollPane1 = new JScrollPane();
		jScrollPane1.setViewportView(lstDocentes);
		// =============================== ===================================
		
		// ========================================Design Grouping=====================================

        chkCopy = new javax.swing.JCheckBox();
        cmbSemestreAnterior = new javax.swing.JComboBox();

        chkCopy.setText("Copiar do Semestre Anterior");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblModulo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbModulos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtFile, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addComponent(btnOpenFile))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(chkCopy)
                        .addGap(18, 18, 18)
                        .addComponent(cmbSemestreAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(85, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblModulo)
                    .addComponent(cmbModulos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnOpenFile))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chkCopy)
                    .addComponent(cmbSemestreAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(196, Short.MAX_VALUE))
        );        
		// =============================== ===================================
		
//		add(lblModulo);
//		add(cmbModulo);
//		add(lblImportFile);
//		add(txtFile);
//		add(btnOpenFile);
//		add(lstDocentes);

	}

	public void actionPerformed(ActionEvent e) {
		// File Chooser for file to import

		if (e.getSource() == btnOpenFile) {
			int returnval = fileChooser.showOpenDialog(Step1Module2.this);

			if (returnval == JFileChooser.APPROVE_OPTION) {
				File file = fileChooser.getSelectedFile();
				txtFile.setText(file.getName());
				if (!swingAdapter.setImportFile(txtFile.getText())) {
					// Not valid file
					JOptionPane.showMessageDialog(this, "Extension of submitted file is not supported, please choose another file", "Invalid File",
							JOptionPane.WARNING_MESSAGE);
					txtFile.setText("");
				}
			}

			// Select option on version

		}
	}
	
	private void fillComboBox(JComboBox cmbBox, List<String> optionList) {

		cmbBox.removeAllItems();

		if (optionList != null) {
			Iterator<String> i = optionList.iterator();
			while (i.hasNext()) {
				String item = i.next();
				cmbBox.addItem(item);

			}
		}

	}	
	public DefaultListModel fillList(DefaultListModel listModel, List<String> lstOptions) {

		if (lstOptions != null) {
			Iterator<String> i = lstOptions.iterator();
			while (i.hasNext()) {
				String item = i.next();
				
				listModel.addElement(item);
			}
		}
		return listModel;

	}
	
	public JList getLstDocentes() {
		return lstDocentes;
	}
	
	public JTextField getTxtFile() {
		return txtFile;
	}
	
	private void lstDocentesValueChanged(javax.swing.event.ListSelectionEvent evt) {
        // TODO add your handling code here:

        int index = lstDocentes.getSelectedIndex();
		System.out.println(index);
    }
}